import sys

if len(sys.argv)<1:
    exit()

fp = open(sys.argv[1],'r')
rules = []
for x in fp.readlines():
    if x[:-1]:
        rules.append(x[:-1])
fp.close()

non_terminal_dict = {}

state = 0
key = ''
expr = []
all_token = []

for x in rules:
    if state == 0:
        if x.strip(' ') == '@':
            key = ''
            expr = []
            state = 1
    elif state == 1:
       key = x.strip(' ')
       state = 2
       all_token += [key]
    elif state == 2:
        if x.strip(' ') == '@':
            non_terminal_dict[key] = expr
            state = 0
        else:
            t = x.strip(' ').split(' ')
            all_token += t
            expr.append(t)

nt_tokens = sorted(list(non_terminal_dict.keys()))
all_token = sorted(list(set(all_token)))

def is_nt_token(t):
    if t in nt_tokens:
        return True
    else:
        return False


def token_first(t):
    f = list()
    if not is_nt_token(t):
        return [t]
    else:
        rules = non_terminal_dict[t]
        for r in rules:
            empty = True
            for tk in r:
                if empty:
                    tmp = token_first(tk)
                    if 'NULL' in tmp:
                        empty = True
                        tmp.remove('NULL')
                        f += tmp
                    else:
                        empty = False
                        f += tmp
                else:
                    break
            if empty:
                f.append('NULL')
            f = list(set(f))
        return f

print('Token                        FIRST')
for tk in all_token:
    print('%-30s'%tk + '\t{}'.format(token_first(tk)))